import 'elix/define/Dialog.js'
import 'elix/define/AutoCompleteComboBox.js'
import pwsdHtml from './pwsd.html'
const $click = (element, handler) => {
    element.addEventListener('click', handler)
    element.addEventListener('touchend', handler)
  }
export class pwsdpage extends HTMLElement{
    constructor(svgShow,init){
        super()
        const template = document.createElement('template')
        template.innerHTML = pwsdHtml
        this.svgShow = svgShow
        this.svgShow.$svgShow.append(template.content.cloneNode(true))
        this.$docDialog = this.svgShow.$svgShow.querySelector("#pwsd_box")
        this.$container = this.svgShow.$svgShow.querySelector("#container2")
        this.$password = this.svgShow.$svgShow.querySelector("#password")
        this.$usr = this.svgShow.$svgShow.querySelector("#user")
        if(init){
            this.init()
        }
    }

    open(ykpage){
        svGShow.ykpage = ykpage
        this.$docDialog.open()
    }

    init(){
        var that = this
        var tmp = document.createElement("template")
        var selectHtml = `<elix-auto-complete-combo-box  class="ipt"  id="username" aria-label="Pet">`
        var endHtml = `</elix-auto-complete-combo-box>`
        fetch(window.serviceUrl + "/sys/user", {
            method: "get",
        }).then((res => res.json().then(json => {
            if(json.code == "100000"){
                for(var i=0;i<json.data.length;i++){
                    var val = json.data[i]
                    selectHtml += `<div value="${val.value}">${val.value}</div>`
                }
                selectHtml += endHtml
                tmp.innerHTML = selectHtml
                that.$usr.append(tmp.content.cloneNode(true))
                this.$username = this.svgShow.$svgShow.querySelector("#username")
            }
        })))
        
        this.$ok = this.svgShow.$svgShow.querySelector('#pwdconf')
        var okFunction = function(){
            var data = new FormData()
            data.append("username",window.parent.EncryptionCore(that.$username.value));
            data.append("password",window.parent.EncryptionCore(that.$password.value));
            if(svGShow.lastName && svGShow.lastName == that.$username.value){
                alert("监护人账号不能与操作员账号相同")
                return;
            }
            // if(svGShow.ykpage&&svGShow.ykpage.ykStep==1){
            //     svGShow.ykpage.ykStep =2
            //     svGShow.ykpage.$pwsd2.className = "cls_button_clkd"
            //     svGShow.ykpage.$pwsd2.value +="成功"
            //     svGShow.ykpage.$pwsd2.disabled = true
            //     svGShow.ykpage.$ykyz.disabled = false
            // }else if(svGShow.ykpage&&svGShow.ykpage.ykStep==0){
            //     svGShow.lastName =  that.$username.value
            //     svGShow.ykpage.ykStep =1
            //     svGShow.ykpage.$pwsd1.className = "cls_button_clkd"
            //     svGShow.ykpage.$pwsd1.value +="成功"
            //     svGShow.ykpage.$pwsd1.disabled = true
            //     svGShow.ykpage.$pwsd2.disabled = false
            // }
            // that.$docDialog.close()
            fetch(window.serviceUrl + "/verify/password", {
                method: "POST",
                body: data,
            }).then((res => res.json().then(json => {
                if(json.code == "100000"){
                    if(svGShow.ykpage&&svGShow.ykpage.ykStep==1){
                        svGShow.ykpage.ykStep =2
                        //我这里强制到第三部
                        svGShow.ykpage.$pwsd2.className = "cls_button_clkd"
                        svGShow.ykpage.$pwsd2.value +="成功"
                        svGShow.ykpage.$pwsd2.disabled = true
                        svGShow.ykpage.$ykyz.disabled =svGShow.yaokongyuzhiBol
                        if(svGShow.yaokongyuzhiBol){
                            svGShow.ykpage.ykStep =3
                            svGShow.ykpage.$ykzx.disabled =!svGShow.yaokongyuzhiBol
                        }
                        
                       
                        // svGShow.ykpage.$ykyz.disabled = true
                    }else if(svGShow.ykpage&&svGShow.ykpage.ykStep==0){
                        svGShow.lastName =  that.$username.value
                        svGShow.ykpage.ykStep =1
                        svGShow.ykpage.$pwsd1.className = "cls_button_clkd"
                        svGShow.ykpage.$pwsd1.value +="成功"
                        svGShow.ykpage.$pwsd1.disabled = true
                        svGShow.ykpage.$pwsd2.disabled = false
                    }
                    that.$docDialog.close()
                }else{
                    alert(json.message)
                    //错误提示后也要关掉
                    that.$docDialog.close()
                }
            })))
        }
        $click(this.$ok, okFunction)
        
        var closeFunction = function(){
            if(svGShow.ykpage&&svGShow.ykpage.ykStep==0){
                svGShow.lastName = null
            }
            that.$docDialog.close()
        }
        this.$cancle = this.svgShow.$svgShow.querySelector('#pwdcs')
        this.$close = this.svgShow.$svgShow.querySelector('.closeDoc1')
        $click(this.$close, closeFunction)
        $click(this.$cancle, closeFunction)
    }

}
customElements.define('pwsd-dialog', pwsdpage)

